<?php
namespace Home\Controller;
class AudioController extends CommonController
{
	public function _initialize()
	{
		$this->Is_login();        //检测用户登录信息,如果错误直接返回错误,终止程序
		$this->Is_power("audio");        //检测用户权限,如果错误直接返回错误,终止程序
		$this->assign('heardadminData', session('heardadminData'));
	}

	//添加试听音频
	public function audio_add(){
		if(IS_POST){
			$data = I('post.');
			if(empty($data['audio_title']) || empty($data['uuid']) || empty($data['u2'])){
				$this->ApiError('参数错误','888888');
			}
			$user_info = M('member')->where(['uuid'=>$data['uuid']])->find();
			if(!$user_info){
				$this->ApiError('主播不存在！','888888');
			}

			$style = trim($user_info['feellabel'].','.$user_info['stylelabel'],',');

			$add['audio_title'] = $data['audio_title'];
			$add['sex'] = $user_info['sex'];
			$add['uuid'] = trim($data['uuid']);
			$add['style'] = $style;
			$add['cid'] = $user_info['soundtype'];
			$add['age'] = $user_info['agelabel'];
			$add['audio_link'] = $data['u2'];
			$add['add_time'] = date('Y-m-d H:i:s');
			$res = M('audio')->add($add);
			if($res){
				$this->ApiSuccess('操作成功');
			}else{
				$this->ApiError('添加失败','999999');
			}
		}else{
			$age_style = M('userlabel')->where(['type'=>1])->field('id,labelname')->select();
			$style = M('userlabel')->where(['type'=>['in',[2,3]]])->field('id,labelname')->select();
			$this->assign('style',$style);
			$this->assign('age_style',$age_style);
			$this->display();
		}
	}

	//音频列表
	public function audio_list(){
		$this->p = I('get.p')?I('get.p'):1;
		$type = I('get.type',0,'intval');
		$keyword = I('get.keyword');
		if($type){
			$where['cid'] = $type;
		}
		if($keyword){
			$where['a.audio_title'] = array('like',"%$keyword%");
		}
		$db_audio = M('audio a');
		$count = $db_audio->where($where)->count();
		$p = getpage($count, 20);

		$audio_list = $db_audio
			->field('id,audio_title,uuid,sex,age,style,a.cid,audio_link,add_time,status,cid')
			->where($where)->order("a.add_time DESC")
			->limit($p->firstRow, $p->listRows)
			->select();
		$style_list = M('userlabel')->getField('id,labelname',true);
		foreach ($audio_list as &$vo) {
			$vo['style'] = $this->Parameterexplode($vo['style']);
			$vo['age'] = $this->Parameterexplode($vo['age']);
			$vo['audio_link'] =url_add_domainname($vo['audio_link']);
			$vo['style_name'] = '';
			foreach($vo['style'] as $s){
				$vo['style_name'] .= $style_list[$s].',';
			}
			foreach($vo['age'] as $a){
				$vo['age_name'] .= $style_list[$a].',';
			}
			$vo['soundtype'] = explode(",", $vo['cid']);
			$vo['soundtype_name'] = str_replace(array(0,1,2,3,4,5,6),array("讲述类","情感类","演绎类","新闻类","英文类","自选","背景音"),$vo['soundtype']);
			$vo['soundtype_name'] = implode(',',$vo['soundtype_name']);
			$vo['style_name'] = rtrim($vo['style_name'],',');
			$vo['age_name'] = rtrim($vo['age_name'],',');
			$vo['typename'] = $vo['status'] == 1 ? '<span style="color:green">显示</span>' : '<span style="color:red">隐藏</span>';
		}
		$map['type'] = empty($type)?"":$type;
		$page = $p->show();
		$p->parameter = "";
		foreach($map as $key=>$val) {
			$p->parameter .= "$key=".urlencode($val)."&";
		}
		$this->audio_list = $audio_list;
		$this->type = $type;
		$this->keyword = $keyword;
		$this->assign('page', $page);
		$this->display();
	}

	//修改显示隐藏
	public function showhideaudio(){
		$status = I('get.status');
		$id = I('get.id',0,'intval');
		if(!$id){
			$this->error('请求错误');
		}
		$save['status'] = $status == 1 ? 0 : 1;
		$res = M('audio')->where(array('id'=>$id))->save($save);
		if ($res) {
			$this->success('修改成功');
		} else {
			$this->error('修改失败');
		}
	}

	//获取用户的风格
	public function get_member_style(){
		$uuid = I('post.uuid');
		$member_info = M('member')->where(['uuid'=>$uuid])->field('sex,soundtype,agelabel,feellabel,stylelabel')->find();
		if($member_info){
			$age = $member_info['agelabel'];
			$style = trim($member_info['feellabel'].','.$member_info['stylelabel'],',');
			$age1 = $this->Parameterexplode($age);
			$style1 = $this->Parameterexplode($style);
			$agename = '';
			$stylename = '';
			$style_list = M('userlabel')->getField('id,labelname',true);
			foreach($age1 as $a){
				$agename .= $style_list[$a].',';
			}
			foreach($style1 as $s){
				$stylename .= $style_list[$s].',';
			}
			switch ($member_info['sex']){
				case 1: $member_info['sex_name'] = '男';break;
				case 2: $member_info['sex_name'] = '女';break;
			}
//			switch ($member_info['soundtype']){
//				case 1: $member_info['soundtype'] = '男';break;
//				case 2: $member_info['soundtype'] = '女';break;
//			}
			$soundtype_arr = explode(",", $member_info['soundtype']);
			$soundtype_arr1 = str_replace(array(0,1,2,3,4,5,6),array("讲述类","情感类","演绎类","新闻类","英文类","自选","背景音"),$soundtype_arr);
			$member_info['soundtype_name'] = implode(",", $soundtype_arr1);
			$return['code'] = '000000';
			$return['agename'] = rtrim($agename,',');
			$return['stylename'] = rtrim($stylename,',');
			$return['sex_name'] = $member_info['sex_name'];
			$return['soundtype_name'] = $member_info['soundtype_name'];
			$this->ajaxReturn($return);
		}else{
			$return['code'] = '999999';
			$return['message'] = '主播不存在';
			$this->ajaxReturn($return);
		}
	}
}